#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget),
    wthread(this->cond,this->rwlock,this->msg),
    rthred1(this->cond,this->rwlock,this->msg),
    rthread2(this->cond,this->rwlock,this->msg)

{
    ui->setupUi(this);
    flag = true;//线程正在停止

}

Widget::~Widget()
{
    delete ui;
}

void Widget::on_pushButton_clicked()
{
    if(flag){
        flag = false;
        ui->pushButton->setText("stop");
        wthread.start();

        rthred1.start();
        rthread2.start();
    }else{
        flag = true;
        ui->pushButton->setText("start");
        wthread.stop();
        rthred1.stop();
        rthread2.stop();
         wthread.wait();
          rthred1.wait();
        rthread2.wait();
        // wthread.stop(); wthread.wait();  rthred1.stop();rthred1.wait();
    }
}
void Widget::closeEvent(QCloseEvent *event){
    if(wthread.isRunning()){
        wthread.stop();
    }
    if(rthred1.isRunning()){
       rthred1.stop();
    }
    if(rthread2.isRunning()){
        rthread2.stop();
    }
     wthread.wait();
     rthred1.wait();
     rthread2.wait();
}
